<?php include "include/function_library.php"; ?>
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>

<?php include "html/header.html"; ?>

</head>
<body class="tcuk home">

	<div id="wp">
	
		<?php  include "html/menu.html"; ?>
		
		<!-- CONTENT STARTS-->
		<div id="ct">	
			
			<?php 
				if(get("edited") == "1") echo "<div class='alert success'>Hotel Edited Successfully!</div>";
				if(get("purchased") == "1") echo "<div class='alert success'>Hotel Ticket Purchased Successfully!</div>";
				
				$id = get("id");
				
				if(get("rate")) {
					if(session("id")) {
						$user_rate = get("rate");
						$rate_user_id = session("id");
						
						$is_rated_before = sql_number("SELECT * FROM rate WHERE user_id='$rate_user_id' AND hotel_id='$id'");
						
						if($is_rated_before > 0) {
							echo "<div class='alert error'>Error! You already rated before!</div>";
						} else {
							$query = "INSERT INTO rate VALUES ('$rate_user_id', '$id', '$user_rate')";
							$result = sql_query($query);
							
							if(!$result) {
								echo "<div class='alert error'>Error! Could not save your rate.</div>";
							} else {
								echo "<div class='alert success'>Your Rate Saved Successfully!</div>";
							}
						}
														
					} else {
						echo "<div class='alert error'>Error! Only login users can rate.</div>";
					}
				}
				
				$query = "select * from hotel_with_address WHERE id=$id"; 
				$result = sql_query($query);
				$tuple = mysql_fetch_assoc($result);
				?>
				
				<table style="width:80%;height:100%;">
					<tr>
						<td><img alt="" src="resource/Voyage_Sorgun_01.jpg" width="500px"></td>			
						<td>
							<h2><?php echo $tuple["name"]; ?></h2><br />
							<ul>
								<li><b>Type :</b> <?php echo $tuple["type"]; ?></li>
								<li><b>Capacity :</b> <?php echo $tuple["capacity"]; ?></li>
								<li><b>Address :</b> <?php echo $tuple["city"]; ?> / <?php echo $tuple["district"]; ?></li>
								<li><b>Phone :</b> <?php echo $tuple["tel"]; ?></li>
								<li><b>Total Rate :</b> <?php echo $tuple["total_rate"]; ?></li>
								<li><b>Price :</b> <?php echo $tuple["price"]; ?> TL</li>
								
							</ul>
						</td>
					</tr>
					<tr>
						<td colspan="2">
							<h3>Details</h3><br/>
							<p><?php echo $tuple["description"]; ?></p>
						</td>
					</tr>
					
					<tr>
						<td colspan="3" style="text-align:right">
							<div>
								<script type="text/javascript">
									function rateJs() {
										window.location = "hotel_detail.php?id=<?php echo $tuple["id"]; ?>&rate=" + $("#rate_level").val();
									}
								</script>
								<select id="rate_level" name="rate_level">
									<option value="0">0 - awful</option>
									<option value="1">1 - bad</option>
									<option value="2">2 - not good</option>
									<option value="3">3 - not bad</option>
									<option value="4">4 - good</option>
									<option value="5">5 - very good</option>
								</select>
								<a href="javascript:rateJs();"><button>Rate</button></a>
								<?php
								if(session("is_customer") == "0") {
									?>
									<a href="edit_hotel.php?id=<?php echo $tuple["id"]; ?>"><button>Edit</button></a>
									<a href="delete_hotel.php?id=<?php echo $tuple["id"]; ?>"><button>Delete</button></a>
									<?php
								} else {
									?>
									<a href="purchase_hotel.php?id=<?php echo $tuple["id"]; ?>"><button>Purchase</button></a>
									<?php
								}
								?>
							</div>
						</td>
					</tr>
					
				</table>
				
				<hr width="80%"/>
				
				<div id="comment-box">
					<?php
						if(post("comment_sent")) {
							if(session("id")) {
								$user_comment = post("user_comment");
								$comment_user_id = session("id");
								if($user_comment != "") {
									$query = "INSERT INTO comment VALUES ('$comment_user_id', '$id', '$user_comment')";
									$result = sql_query($query);
									if(!$result) {
										echo "<div class='alert error'>Error! Could not save your comment.</div>";
									}
								} else {
									echo "<div class='alert error'>Error! You can not send empty comments.</div>";
								}
							} else {
								echo "<div class='alert error'>Error! Only login users can comment.</div>";
							}
						} 
						
						$query = "SELECT COUNT(*) as comment_count FROM comment WHERE hotel_id=$id";
						$result = sql_query($query);
							
						$tuple = mysql_fetch_assoc($result);
						
						$comment_count = "";
						
						if($tuple["comment_count"] > 1) {
							$comment_count = "There are ".$tuple["comment_count"]." comments made for this hotel."; 
						} elseif($tuple["comment_count"] == 1) {
							$comment_count = "There is only one comment made for this hotel.";
						} else {
							$comment_count = "Anyone has not comment for this hotel yet. <br />You can be first!";
						}
					?>
					<div id="comment-header">
						You can checkout comments of former visitors for this hotel<br/>
						Or you can comment for this hotel using our comment form<br />
						<?php echo $comment_count; ?>
					</div>
					
					<?php 
					$query = "SELECT c.comment as c_comment, u.id as user_id, u.name as name, u.surname as surname FROM comment c, user u WHERE c.user_id = u.id AND c.hotel_id=$id"; 
					$result = sql_query($query);
					
					while($tuple = mysql_fetch_assoc($result)) {
						?>
						<div class="single-comment">
							<h4><b><?php echo $tuple["name"]." ".$tuple["surname"]; ?></b></h4>
							<p><?php echo $tuple["c_comment"]; ?></p>
						</div>
						<?php
					}
					?>
					<div id="comment-form">
						<form action="<?php echo curPageURL(); ?>?id=<?php echo $id; ?>" method="post">
							<textarea name="user_comment" placeholder="Write Your Comment Here! (3000 characters)" maxlength="3000" style="width: 100%; height: 50px;"></textarea>
							<input type="submit" name="comment_sent" value="Send Comment" />
						</form>
					</div>
				
				</div>
		
		</div> 	
		<!-- CONTENT ENDS-->		
		
		<?php include "html/footer.html"; ?>
		
	</div>
	
</body>
</html>